import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/req/match/req_match.dart';
import 'package:flutter_changdian_football/base/base_page_controller.dart';
import 'package:flutter_changdian_football/config/enum/refresh.dart';
import 'package:flutter_changdian_football/model/match/user_match_info_model.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:flutter_changdian_football/utils/event/custom_event_util.dart';
import 'package:flutter_changdian_football/utils/ext/refresher_extension.dart';
import 'package:flutter_changdian_football/widget/toast/custom_toast.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class EnrollmentWidgetController extends BaseGetPageController {

  List<UserMatchInfoModel> userMatchInfoList = [];

  @override
  void requestData(
    RefreshController controller, 
    {
      Refresh refresh = Refresh.first
    }
  ) async {
    var response = await ReqMatch.getUserMatchInfo(
      matchType: 'SIGN_UP'
    );

    if (response.status == Status.completed) {
      if (response.data['code'] == '200') {
        /// 判断是否为最后一页, 如果是， over传true， 否则false
        RefreshExtension.onSuccess(controller, refresh, true); 

        /// 下拉刷新需要清除列表
        if (refresh != Refresh.down) {
          userMatchInfoList.clear();
          update();
        }

        for (int i = 0; i < response.data['data'].length; i++) {
          userMatchInfoList.add(UserMatchInfoModel.fromJson(response.data['data'][i]));
        }

        showSuccess(userMatchInfoList);
        update();
      } else {
        showError();  
        RefreshExtension.onError(controller, refresh);
      }
    } else {
      showError();  
      RefreshExtension.onError(controller, refresh);
    }
  }

  @override
  void onInit() {
    super.onInit();

    eventBus.on<BottomNavigationBarEvent>().listen((event) {
      if (event.message == CustomConstant.EVENT_MATCH_REFRESH) {
        EasyLoading.show();
        // 刷新数据
        requestData(controller!);
      } else if (event.message == CustomConstant.EVENT_LOGOUT_REFRESH) {
        clearData();
      } else if (event.message == CustomConstant.EVENT_REFRESH) {
        EasyLoading.show();
        // 刷新数据
        requestData(controller!);
      }
    });
  }

  clearData() {
    userMatchInfoList.clear();
    update();
  }

  /// 比赛报名
  signUp(matchId) async {
    try {
      EasyLoading.show();

      var resp = await ReqMatch.signUp(
        matchId: '$matchId'
      );
      if (resp.status == Status.completed) {
        if (resp.data['code'] == '200') {
          CustomToast.showTextToast('操作成功');
          eventBus.fire(const BottomNavigationBarEvent(CustomConstant.EVENT_MATCH_REFRESH));
        } else {
          CustomToast.showTextToast(resp.data['msg']);
        }
      } else {
        CustomToast.showTextToast('系统繁忙，请稍后再试！');
      }
    } catch (e) {
      CustomToast.showTextToast('系统繁忙，请稍后再试！');
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 取消报名
  cancelSignUp(matchId) async {
    try {
      EasyLoading.show();

      var resp = await ReqMatch.cancelSignUp(
        matchId: '$matchId'
      );
      if (resp.status == Status.completed) {
        if (resp.data['code'] == '200') {
          CustomToast.showTextToast('操作成功');
          eventBus.fire(const BottomNavigationBarEvent(CustomConstant.EVENT_MATCH_REFRESH));
        } else {
          CustomToast.showTextToast(resp.data['msg']);
        }
      } else {
        CustomToast.showTextToast('系统繁忙，请稍后再试！');
      }
    } catch (e) {
      CustomToast.showTextToast('系统繁忙，请稍后再试！');
    } finally {
      EasyLoading.dismiss();
    }
  }

}